package ltu.temp;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import android.util.Log;

public class XmlParser {

	public ArrayList fetchLocation(){
		ArrayList<String> allTemp = new ArrayList();
		
		String url="http://www.temperatur.nu/api/tnu_1.1.php?simple";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.parse(new URL(url).openStream());
		
		doc.getDocumentElement().normalize();
		
		NodeList nList = doc.getElementsByTagName("item");
		
		Log.d( "Root element :", doc.getDocumentElement().getNodeName());
		
		for (int temp = 0; temp < nList.getLength(); temp++) {
			 
			   Node nNode = nList.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
	 
			      Element eElement = (Element) nNode;
	 
			      Log.d("First Name : ",  getTagValue("title", eElement));
			      Log.d("Last Name : ",  getTagValue("temp", eElement));
			      allTemp.add(getTagValue("title", eElement) + " " + getTagValue("temp", eElement) );
			   }
			}
		  } catch (Exception e){}
		return allTemp;
			
	}
	
	 private static String getTagValue(String sTag, Element eElement) {
				NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		 
		        Node nValue = nlList.item(0);
		 
		  return nValue.getNodeValue();
		  }
}
